﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace cs499.Database
{
    public partial class TimeSlot
    {

        public bool Delete()
        {
            bool retval = true;
            Model1Container db = new Model1Container();
            TimeSlot timeslot = db.Set<TimeSlot>()
                .Include("CourseInstances")
                .FirstOrDefault(timetofind => timetofind.Id == this.Id);

            foreach (CourseInstance ci in timeslot.CourseInstances.ToList())
            {
                db.Set<CourseInstance>().Remove(ci);
            }
            try
            {
                db.Set<TimeSlot>().Remove(timeslot);
                db.SaveChanges();
            }
            catch (Exception /**/)
            {
                return false;
            }
            return retval;
        }

        public override string ToString()
        {
            System.Text.StringBuilder timeSlot = new System.Text.StringBuilder();

            //add in the start time in 12hr format
            timeSlot.Append(DateTime.ParseExact(StartTime.ToString(), "HH:mm:ss", null).ToString("hh:mmtt"));

            //add in the dash to indicate start to finish
            timeSlot.Append(" - ");

            //calculate the end time
            TimeSpan endTime = StartTime.Add(new TimeSpan(0, Length, 0));

            //add in the end time in 12hr format
            timeSlot.Append(DateTime.ParseExact(endTime.ToString(), "HH:mm:ss", null).ToString("hh:mmtt"));

            //add a space between time and date(s)
            timeSlot.Append(" ");

            //add in the relevant dates
            if ((Days & DayOfWeek.Monday) == DayOfWeek.Monday)
                timeSlot.Append("M");
            if ((Days & DayOfWeek.Tuesday) == DayOfWeek.Tuesday)
                timeSlot.Append("T");
            if ((Days & DayOfWeek.Wednesday) == DayOfWeek.Wednesday)
                timeSlot.Append("W");
            if ((Days & DayOfWeek.Thursday) == DayOfWeek.Thursday)
                timeSlot.Append("R");
            if ((Days & DayOfWeek.Friday) == DayOfWeek.Friday)
                timeSlot.Append("F");
            if ((Days & DayOfWeek.Saturday) == DayOfWeek.Saturday)
                timeSlot.Append("S");
            if ((Days & DayOfWeek.Sunday) == DayOfWeek.Sunday)
                timeSlot.Append("U");

            return timeSlot.ToString();
        }

        public static string DaystoString(DayOfWeek Days)
        {
            System.Text.StringBuilder outstr = new System.Text.StringBuilder();
            if ((Days & DayOfWeek.Monday) == DayOfWeek.Monday)
                outstr.Append("M");
            if ((Days & DayOfWeek.Tuesday) == DayOfWeek.Tuesday)
                outstr.Append("T");
            if ((Days & DayOfWeek.Wednesday) == DayOfWeek.Wednesday)
                outstr.Append("W");
            if ((Days & DayOfWeek.Thursday) == DayOfWeek.Thursday)
                outstr.Append("R");
            if ((Days & DayOfWeek.Friday) == DayOfWeek.Friday)
                outstr.Append("F");
            if ((Days & DayOfWeek.Saturday) == DayOfWeek.Saturday)
                outstr.Append("S");
            if ((Days & DayOfWeek.Sunday) == DayOfWeek.Sunday)
                outstr.Append("U");

            return outstr.ToString();
        }

        public string strvalue
        {
            get
            {
                return this.ToString();
            }
        }


        public bool OverlapsWith(TimeSlot atimeslot)
        {
            //if (this.Id == atimeslot.Id) return true;
            if ((this.Days & atimeslot.Days) != 0)
            {
                if (atimeslot.StartTime.TotalMinutes < this.StartTime.TotalMinutes + this.Length && atimeslot.StartTime.TotalMinutes + this.Length > this.StartTime.TotalMinutes)
                {
                    return true;
                }
                else return false;
            }
            else return false;
        }
    }
}